/*
 * @Author: silas 
 * @Date: 2021-05-28 14:27:01 
 * @Last Modified by: silas
 * @Last Modified time: 2021-05-28 17:34:16
 */
<template>
    <div class="compilationSummary">
        <div>
            <el-form
                :inline="true"
                :model="form"
                label-width="100px"
                class="formList"
            >
                <el-form-item label="账期" size="mini">
                    <el-date-picker v-model="form.time" type="month" value-format="yyyyMM"></el-date-picker>
                </el-form-item>
                <el-form-item size="mini" style="width: 19%;">
                    <div class="g-btn-wrapper">
                        <div class="g-cell">
                            <el-button
                                size="mini"
                                style="margin-left: 12px"
                                type="primary"
                                @click="search"
                                >查询</el-button
                            >
                            <el-button
                                size="mini"
                                type="orange"
                               @click="
                                    dialogVisible = true;
                                    importForm.year = '';
                                    fileList = [];
                                "
                                >导入</el-button
                            >
                        </div>
                    </div>
                </el-form-item>
            </el-form>
        </div>
        <div class="table-content clearfix">
            <div class="tableBox">
                <el-table
                    v-loading="tableLoading"
                    :data="tableData"
                    style="width: 100%"
                    class="tableTh28"
                    height="73vh"
                    stripe
                >
                    <el-table-column
                        label="区域"
                        width="120"
                        prop="areaName"
                    />
                    <el-table-column
                        label="账期"
                        width="120"
                        prop="acctMonth"
                    />
                    <el-table-column
                        label="文件名"
                        min-width="120"
                        prop="attachName"
                    />
                    <el-table-column
                        label="导入人名称"
                        width="140"
                        prop="userName"
                    />
                    <el-table-column
                        label="操作"
                        width="160"
                    >
                        <template slot-scope="scope">
                             <el-link type="primary" @click="handle_del(scope.row,scope.$index)">删除</el-link>
                             <el-link type="primary" @click="handle_down(scope.row)">下载</el-link>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
        <el-dialog
            title="导入文件"
            append-to-body
            :visible.sync="dialogVisible"
            width="450px"
        >
            <el-form :model="importForm" ref="importForm" :rules="rules">
                <el-form-item label="账期" prop="year" label-width="80px">
                    <el-date-picker
                        v-model="importForm.year"
                        type="month"
                        size="mini"
                        placeholder="选择时间"
                        value-format="yyyyMM"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="上传文件" prop="file">
                     <el-upload
                        size="mini"
                        :limit="1"
                        accept=".xls, .xlsx"
                        class="avatar-uploader"
                        :auto-upload="false"
                        :file-list="fileList"
                        :on-change="changeFile"
                        action
                    >
                        <el-button size="mini" type="primary"
                            >点击上传</el-button
                        >
                    </el-upload>
                </el-form-item>
            </el-form>
            <div slot="footer">
                <div class="dialog-footer">
                    <el-button
                        size="mini"
                        type="primary"
                        @click="handle_import()"
                        >确定</el-button
                    >
                    <el-button size="mini" @click="dialogVisible = false"
                        >取消</el-button
                    >
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import prefixAPI from "@/api/prefixAPI";
import { getCookie } from "@/common/js/cookie";
import { getSessionStorage } from "@/common/js/storage";
import { $budgetAPI } from "@/api";
import FileSaver from "file-saver";
import XLSX from "xlsx";
const exportUrl = prefixAPI.pack;
export default {
    name: "constructionUse",
    data() {
        return {
            form: {
                time: '',
            },
            tableData: [],
            tableLoading: false,
            status: [],
            importForm: {
                year: "",
            },
            dialogVisible: false,
            fileList: [],
            rules: {
                year: [
                    { required: true, message: "请选择时间", trigger: "blur" },
                ],
            },
        };
    },
    watch: {
       
    },
    created() {
        var that = this;

        that.accountInfo = JSON.parse(getSessionStorage("accountInfo"));
        that.form.areaId = that.accountInfo.areaId;
        that.form.time = that.moment().format("YYYYMM");
        // that.getMonth();
    },
   
    methods: {
        //查询
        search() {
            this.getData();
        },
        getData() {
            var that = this;

            that.tableLoading = true;
            $budgetAPI.getProjectUnit(
                {
                    acctMonth: that.form.time,
                },
                (res) => {
                    if (res && res.code == 0) {
                        that.tableData = res.content;
                    }
                    that.tableLoading = false;
                }
            );
        },
        progress(event, file, fileList) {
            this.loading = this.$loading({
                lock: true,
                text: "正在导入",
                spinner: "el-icon-loading",
                background: "rgba(0, 0, 0, 0.7)",
            });
        },
        async init() {
            let that = this;
            
            that.getData();
        },
        handle_export() {
            var ysgldwG = this.form.unit,
                jsdwAh = this.form.construction,
                tzqdQ = this.form.channel,
                tzsdI = this.form.years,
                gcztY = this.form.status,
                xqglybmAb = this.form.person1,
                gcglybmAg = this.form.person2,
                month = this.form.month,
                token = getCookie("token");

            window.open(`${exportUrl}/builderYjzy/exportBuilderYjzyDataList?ysgldwG=${ysgldwG}&jsdwAh=${jsdwAh}&tzqdQ=${tzqdQ}&tzsdI=${tzsdI}&gcztY=${gcztY}&xqglybmAb=${xqglybmAb}&gcglybmAg=${gcglybmAg}&acctMonth=${month}&token=${token}`, "_self");
        },
        handle_del(row) {
            var that = this;

            that.$confirm("是否删除?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                $budgetAPI.delProgrammer(
                    {
                        recordId: row.recordId,
                        token: getCookie("token"),
                    },
                    (res) => {
                        if (res.code == 0) {
                            this.$message({
                                type: "success",
                                message: "删除成功!",
                            });
                        } else {
                            this.$message({
                                type: "error",
                                message: res.message,
                            });
                        }
                        that.getData();
                    }
                );
            });
        },
		handle_down(it) {
            var token = getCookie("token");

            window.location.href =
                prefixAPI.hp +
                "/api/builderImport/downloadFile?recordId=" + it.recordId + "&token=" +
                token;
        },
        // 批量导入上传文件传递数据
        changeFile(file, fileList) {
            this.fileList = fileList;
            this.param = new FormData();
            this.param.append("file", file.raw);
        },
        // 导入
        handle_import(type) {
            var that = this;

            that.$refs["importForm"].validate((valid) => {
                if (valid) {
                    if (that.fileList.length == 0) {
                        that.$message({
                            message: "请选择需要上传的文件",
                            type: "warning",
                        });
                        return false;
                    }
                 
                    that.param.append("token", getCookie("token"));
                    that.param.append("acctMonth", that.importForm.year);
                    this.loading = this.$loading({
                        lock: true,
                        text: "正在导入",
                        spinner: "el-icon-loading",
                        background: "rgba(0, 0, 0, 0.7)",
                    });
                    $budgetAPI.importBuilderProjectData(
                        that.param,
                        (res) => {
                            this.loading.close();
                            if (res.code == 0) {
                                that.$message({
                                    message: "导入成功",
                                    type: "success",
                                });
                                that.getData();
                            } else {
                                that.$message.error(res.message);
                            }
                            that.dialogVisible = false;
                        },
                        (err) => {
                            that.dialogVisible = false;
                        }
                    );
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
    },
};
</script>

<style lang="scss" scoped>
.compilationSummary {
    .formList {
        margin-top: 20px;
        padding-bottom: 20px;
        .el-form-item {
            width: 27%;
            &:last-child {
                width: 19%;
            }
        }
    }
    .g-btn-wrapper {
        display: flex;
        .g-cell {
            flex: 1;
            button {
                padding: 7px 15px;
            }
        }
        .g-btns {
            text-align: right;
        }
    }
    .table-content {
        .table-header {
            display: flex;
            justify-content: space-between;

            .btn-right {
                display: flex;
            }

            .table-title {
                font-size: 14px;
                font-weight: bold;
                height: 30px;
                line-height: 30px;

                span {
                    display: inline-block;
                    width: 5px;
                    height: 5px;
                    background-color: #418ee9;
                    border-radius: 1000%;
                    overflow: hidden;
                    margin-right: 10px;
                    margin-bottom: 3px;
                }
            }
        }
    }
}
</style>
<style lang="scss">
.my {
    // background-color: red;
     .el-tag--info {
        display: inline-block;
        max-width: 80px;
        height: 20px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
}
/deep/ .el-form-item--mini .el-form-item__content {
    line-height: 35px;
}
</style>
